import 'package:flutter/foundation.dart';
import 'package:share_fit/app/controllers/base/base_controller.dart';
import 'package:share_fit/app/tools/log_utils.dart';
import 'package:share_fit/app/ute/uteped_const.dart';

import 'light_state.dart';

class LightLogic extends BaseController {
  final LightState state = LightState();
  static const methodSetScreenAutoLight = "setScreenAutoLight";

  @override
  void onReady() {
    super.onReady();
    // 这个值设备会自动上报，UTEPedDevice也会保存，所以这里进行监听处理
    addStorageListener(methodSetScreenAutoLight, (value) {
      state.lightSwitchValue = value;
      update(['light_switch']);
    });
  }

  void onSetLightRemindEnable(value) {
    showLoading();
    state.lightSwitchValue = value;
    bRemoteReqAsync(methodSetScreenAutoLight, {UTEPedConst.keyEnable: value})
        .then((success) => _onSetScreenAutoLight(success, value));
  }

  void _onSetScreenAutoLight(success, value) {
    cancelLoading();
    if (success) {
      save(methodSetScreenAutoLight, value);
    }
    else {
      state.lightSwitchValue = !value;
    }
  }

  @override
  String getRemotePath() {
    return UTEPedConst.pathDeviceOptions;
  }

  @override
  void initState() {
    state.lightSwitchValue = read(methodSetScreenAutoLight, defaultValue: false);
  }

  @override
  void onRemoteEvent(event, method) {
    // TODO: implement onRemoteEvent
  }
}
